#include "exti.h"

//PA0 PE2 PE3 PE4
void Exti_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	EXTI_InitTypeDef EXTI_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;

	//使能GPIOA组时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
	//使能GPIOE组时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
	
	//使能SYSCFG时钟： 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; //0号引脚
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; //输入模式
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //带上拉电阻
	
	//初始化结构体
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4; //0号引脚
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; //输入模式
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //带上拉电阻
	
	//初始化结构体
	GPIO_Init(GPIOE,&GPIO_InitStruct);
	
	//设置IO口与中断线的映射关系
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource2);
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource3);
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource4);
	
	EXTI_InitStruct.EXTI_Line = EXTI_Line0; //中断线0
	EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; //触发中断
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;  //下降沿触发
	EXTI_InitStruct.EXTI_LineCmd = ENABLE; //使能外部中断
	
	//外部中断初始化
	EXTI_Init(&EXTI_InitStruct);
	
	EXTI_InitStruct.EXTI_Line = EXTI_Line2; //中断线2
	EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; //触发中断
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;  //下降沿触发
	EXTI_InitStruct.EXTI_LineCmd = ENABLE; //使能外部中断
	
	//外部中断初始化
	EXTI_Init(&EXTI_InitStruct);
	
	EXTI_InitStruct.EXTI_Line = EXTI_Line3; //中断线3
	EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; //触发中断
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;  //下降沿触发
	EXTI_InitStruct.EXTI_LineCmd = ENABLE; //使能外部中断
	
	//外部中断初始化
	EXTI_Init(&EXTI_InitStruct);
	
	EXTI_InitStruct.EXTI_Line = EXTI_Line4; //中断线4
	EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; //触发中断
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;  //下降沿触发
	EXTI_InitStruct.EXTI_LineCmd = ENABLE; //使能外部中断
	
	//外部中断初始化
	EXTI_Init(&EXTI_InitStruct);
	
	NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn; //中断通道为中断线0
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2; //响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;  //使能中断配置
	
	//中断分组管理 初始化
	NVIC_Init(&NVIC_InitStruct);
	
	NVIC_InitStruct.NVIC_IRQChannel = EXTI2_IRQn; //中断通道为中断线2
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2; //响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;  //使能中断配置
	
	//中断分组管理 初始化
	NVIC_Init(&NVIC_InitStruct);
	
	NVIC_InitStruct.NVIC_IRQChannel = EXTI3_IRQn; //中断通道为中断线3
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2; //响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;  //使能中断配置
	
	//中断分组管理 初始化
	NVIC_Init(&NVIC_InitStruct);
	
	NVIC_InitStruct.NVIC_IRQChannel = EXTI4_IRQn; //中断通道为中断线4
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2; //响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;  //使能中断配置
	
	//中断分组管理 初始化
	NVIC_Init(&NVIC_InitStruct);
	
}

//中断服务函数 -- 产生中断条件时系统自动调用
void EXTI0_IRQHandler(void)
{
  //判断中断线0的触发
	if(EXTI_GetITStatus(EXTI_Line0)==SET)
	{
		//编写控制程序
	  PFout(9) ^= 1;
	
	}
  EXTI_ClearITPendingBit(EXTI_Line0); //清空标志位
}


void EXTI2_IRQHandler(void)
{
  //判断中断线2的触发
	if(EXTI_GetITStatus(EXTI_Line2)==SET)
	{
		//编写控制程序
	  PFout(10) ^= 1;
	
	}
  EXTI_ClearITPendingBit(EXTI_Line2); //清空标志位
}


void EXTI3_IRQHandler(void)
{
  //判断中断线3的触发
	if(EXTI_GetITStatus(EXTI_Line3)==SET)
	{
		//编写控制程序
	  PEout(13) ^= 1;
	
	}
  EXTI_ClearITPendingBit(EXTI_Line3); //清空标志位
}


void EXTI4_IRQHandler(void)
{
  //判断中断线4的触发
	if(EXTI_GetITStatus(EXTI_Line4)==SET)
	{
		//编写控制程序
	  PEout(14) ^= 1;
	
	}
  EXTI_ClearITPendingBit(EXTI_Line4); //清空标志位
}

